package cn.cheneyjoo.respository;

import cn.cheneyjoo.feigninterface.ProductsFeignInterface;
import cn.cheneyjoo.model.Products;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @author cheneyjooo
 * 监听服务器上的kafka是否有相关的消息发过来
 */
@Component
public class ProductConsumer {
    @Autowired
    private ProductsFeignInterface productsFeignInterface;
    @Autowired
    private ProductRepository productRepository;
    /**
     * 监听消费消息
     * @param record 消息对象本身
     */
    @KafkaListener(topics = "product")
    public void listen (ConsumerRecord<?, ?> record){
        Products product = productsFeignInterface.getOneProduct(Integer.parseInt(record.value().toString()));
        productRepository.save(product);
    }
}
